

#include "gp_stockstruct.h"

namespace gp
{

	namespace
	{
		void ___test()
		{
			const int iStockDate = sizeof(StockDate);
			const int iStockKey = sizeof(StockKey);
			const int iStockPrice = sizeof(StockPrice);
		}
	};

	/****************************************************************8
	 ***** StockData
	 ****************************************************************/

	StockDate::StockDate()
	{
		datetime = 0;
	}
	StockDate::StockDate(const struct tm &t)
	{
		operator=(t);
	}
	StockDate::StockDate(const time_t &t)
	{
		operator=(t);
	}
	StockDate::StockDate(int _year, int _month, int _day, int _hour, int _min, int _sec)
	{
		year = _year;
		month = _month;
		day = _day;
		hour = _hour;
		minute = _min;
		second = _sec;
	}
	StockDate &StockDate::operator=(const struct tm &t)
	{
		year = t.tm_year + 1900 - 2000;
		month = t.tm_mon + 1;
		day = t.tm_mday;
		hour = t.tm_hour;
		minute = t.tm_min;
		second = t.tm_sec;
		return *this;
	}
	StockDate &StockDate::operator=(const time_t &t)
	{
		struct tm t2 = *localtime(&t);
		return operator=(t2);
	}
	StockDate::operator time_t() const
	{
		struct tm t2 = operator struct tm();
		return mktime(&t2);
	}
	StockDate::operator struct tm() const
	{
		struct tm t2;
		t2.tm_year = year + 2000 - 1900;
		t2.tm_mon = month - 1;
		t2.tm_mday = day;
		t2.tm_min = minute;
		t2.tm_sec = second;
		return t2;
	};
	StockDate::operator std::string() const
	{
		return format(true, true);
	}

	std::string StockDate::format(bool isDate, bool isTime) const
	{
		char buf[64];
		int iLen = 0;
		if (isDate)
		{
			iLen = sprintf(buf, "%04d-%02d-%02d"
				,year + 2000,month,day	);
		}
		if (isDate && isTime)
		{
			iLen += sprintf(buf + iLen, " ");
		}
		if (isTime)
		{
			iLen += sprintf(buf + iLen, "%02d:%02d:%02d"
				,hour,minute,second);
		}
		return buf;
	}

	/****************************************************888
	 * StockKey
	 ****************************************************/
	StockKey::StockKey()
	{
		key = 0;
	}
	StockKey::StockKey(int id, unsigned int date)
	{
		stock = id;
		datetime = date;
	}


	/******************************************************
	.	StockPrice
	 ******************************************************/
	INT16 StockPrice::cal_zhangfu(float lastClosePrice)
	{
		if (lastClosePrice == 0) 
		{
			return zhangfu = 0;
		}
		if (price == lastClosePrice || price == 0)
		{
			return zhangfu = 0;
		}

		float k = ((price - lastClosePrice) * 100) / lastClosePrice;
		long i = (long)k;
		if (i > 10000)
		{
			i = 10000;
		}else if (i < -10000)
		{
			i = -10000;
		}
		return zhangfu = (INT16)i;
	}
	void StockPrice::set_zero()
	{
		memset(this, 0, sizeof(*this));
	}
	StockPricePtr StockPrice::NewPtr()
	{
		static mySimpleClsPool<StockPrice, true>	pool;
		return pool.NewPtr();
	}

	/************************************************************8
	 ***** StockInfo
	 ************************************************************/
	StockInfo::StockInfo()
	{
		memset(this, 0, sizeof(*this));
	}
	StockInfo::TStockInfoList &StockInfo::List()
	{
		static TStockInfoList lst;
		return lst;
	}
	StockInfo* StockInfo::Get(int sockID)
	{
		auto &m = List();
		auto it = m.find(sockID);
		if (it == m.end())
		{
			return nullptr;
		}
		return it->second;
	}

	/****************************************************************
	 ****** StockDayList
	 ****************************************************************/
	void StockDayList::sort()
	{

	}
	StockDate StockDayList::next_day(StockDate d) const
	{
		return StockDate();
	}
	StockDate StockDayList::prev_day(StockDate d) const
	{
		return StockDate();
	}


	//--------------------------------
	namespace data
	{
		StockInfo::TStockInfoList &getStockList()
		{
			return StockInfo::List();
		}
		TStockPriceCache &getPriceCache()
		{
			static TStockPriceCache cache;
			return cache;
		}
		StockDayList &getAllDayList()
		{
			static StockDayList lst;
			return lst;
		}
	};
};

